package vumt.radiocountries.dialog;

import java.util.ArrayList;

import vumt.radiocountries.PlayerActivity;
import vumt.radiocountries.R;
import vumt.radiocountries.adapter.ChannelBaseAdapter;
import vumt.radiocountries.object.ChannelObject;
import vumt.radiocountries.object.SubChannelObject;
import vumt.radiocountries.util.ActionItem;
import vumt.radiocountries.util.Global;
import vumt.radiocountries.util.QuickAction;
import vumt.radiocountries.util.QuickAction.OnActionItemClickListener;
import vumt.radiocountries.util.QuickAction.OnDismissListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class DialogListRadioStation extends Activity implements
		OnItemClickListener, OnDismissListener, OnClickListener,
		OnActionItemClickListener {
	ListView lvRadioStation;
	QuickAction quickAction;
	ArrayList<SubChannelObject> listSubChannel;
	private Button btnBack;
	private AdView adView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_listview_radiostation);

		Bundle bundle = this.getIntent().getExtras();
		int position = bundle.getInt("position");
		ChannelObject c = Global.listChannel.get(position);
		listSubChannel = c.getListSubChannel();

		lvRadioStation = (ListView) findViewById(R.id.lvRadioStation);
		ChannelBaseAdapter adapter = new ChannelBaseAdapter(this,
				listSubChannel);
		lvRadioStation.setAdapter(adapter);
		lvRadioStation.setOnItemClickListener(DialogListRadioStation.this);

		quickAction = new QuickAction(this, QuickAction.ANIM_GROW_FROM_RIGHT);

		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		// ADMOB
		adView = (AdView) findViewById(R.id.ad);
		adView.loadAd(new AdRequest());
	}

	ArrayList<String> listLink;
	String currName = "";
	String website = "";

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		quickAction = new QuickAction(this, QuickAction.ANIM_GROW_FROM_RIGHT);
		SubChannelObject subChannel = listSubChannel.get(position);
		currName = subChannel.getName();
		website = subChannel.getWebsite();
		listLink = subChannel.getListLink();

		for (int i = 0; i < listLink.size(); i++) {
			ActionItem nextItem = new ActionItem(i, "Channel " + i,
					getResources().getDrawable(R.drawable.menu_down_arrow));
			quickAction.addActionItem(nextItem);
		}

		quickAction.setOnActionItemClickListener(this);
		quickAction.setOnDismissListener(this);
		quickAction.show(v);
	}

	@Override
	public void onItemClick(QuickAction source, int pos, int actionId) {
		Intent i = new Intent(DialogListRadioStation.this, PlayerActivity.class);
		Global.currLinkRadio = listLink.get(pos);
		i.putExtra("name", currName);
		i.putExtra("website", website);
		startActivity(i);
	}

	@Override
	public void onDismiss() {
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		default:
			break;
		}
	}
}
